package com.peanut.controller;

import com.alibaba.fastjson.JSON;
import com.peanut.common.Result;
import com.peanut.domain.bo.ForumTitleBO;
import com.peanut.domain.po.TabAdmin;
import com.peanut.em.ResultEnum;
import com.peanut.service.ForumCommentService;
import com.peanut.service.ForumTitleService;
import com.peanut.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * mongoDB 车评帖子接口
 */
@RestController
@RequestMapping("forumTitle")
@CrossOrigin
@Slf4j
@Api("车评接口")
public class ForumTitleController {

    @Autowired
    private ForumTitleService forumTitleService;

    @Autowired
    private ForumCommentService commentService;

    //封装验证登录态方法
    private TabAdmin getTabAdmin(String token) {
        TabAdmin tabAdmin = JSON.parseObject(new JwtUtil().parseJwt(token).getSubject(),TabAdmin.class);
        return tabAdmin;
    }

    //发帖，增加，需要验证登录态，登录名为添加人
    @PostMapping("insertTitle")
    @ApiOperation("增加车评帖子")
    public Result insertTitle(ForumTitleBO forumTitleDTO,
                              @RequestHeader("AuthToken") String token){
        //验证用户登录态
        TabAdmin tabAdmin = getTabAdmin(token);
        try{
            if (tabAdmin != null){
                ForumTitleBO bo = new ForumTitleBO();
                //DTO转BO传到service层处理
                BeanUtils.copyProperties(forumTitleDTO,bo);
                boolean b = forumTitleService.insertTitle(bo,tabAdmin);
                return Result.OK().setData(b);
            }
            else {
                //如果未登录，返回未登录状态
                return Result.R(ResultEnum.NO_LOGIN);
            }
        }catch (Exception e){
            e.printStackTrace();
            //增加失败
            return Result.ERROR();
        }
    }

    //查询所有帖子,根据titleName模糊查询，不需要验证登录态
    @GetMapping("getTitles")
    @ApiOperation("按点赞量倒序排序")
    public Result<ForumTitleBO> getTitles(ForumTitleBO forumTitleDTO,
                                          @RequestParam(defaultValue = "0") Integer pageNum,
                                          @RequestParam(defaultValue = "20")Integer pageSize){
        try{
            ForumTitleBO bo = new ForumTitleBO();
            BeanUtils.copyProperties(forumTitleDTO,bo);
            //查询所有帖子
            List<ForumTitleBO> titles = forumTitleService.getTitles(bo,pageNum,pageSize);
            for (ForumTitleBO title : titles) {
                //遍历帖子id查询所有评论+子评论
                List<Map<String, Object>> comments = commentService.getComments(title.getId());
                title.setForumTitle(null);
            }
            return Result.OK().setData(titles);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    //删除帖子，同时删除该贴下的所有评论
    @DeleteMapping("deleteTitle/{id}")
    @ApiOperation("删除车评，同时删除所有评论+子评论")
    @ApiImplicitParam(name = "id",value = "帖子主键",required = true,paramType = "path")
    public Result delete(@PathVariable("id") String id,@RequestHeader("AuthToken") String token){
        //验证用户登录态
        TabAdmin tabAdmin = getTabAdmin(token);
        try{
            if (tabAdmin != null){
                boolean i = forumTitleService.delete(id);
                boolean b = commentService.delete(id);
                return Result.OK();
            } else {
                //如果未登录，返回未登录状态
                return Result.R(ResultEnum.NO_LOGIN);
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    //点赞帖子
    @PutMapping("likeTitle/{id}")
    @ApiOperation("点赞帖子")
    @ApiImplicitParam(name = "id",value = "帖子主键",required = true,paramType = "path")
    public Result likeTitle(@PathVariable("id") String id,@RequestHeader("AuthToken") String token){
        //验证用户登录态
        TabAdmin tabAdmin = getTabAdmin(token);
        try{
            if (tabAdmin != null){
                forumTitleService.likeTitle(id);
                return Result.OK();
            } else {
                //如果未登录，返回未登录状态
                return Result.R(ResultEnum.NO_LOGIN);
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }
}
